package com.china08.yxyapi.api.home.kecheng.topic;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.http.MediaType;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsTopicCatalog;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsTopicLesson;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyLessonsTopicCatalogRepository;
import com.china08.yxyapi.repository.db.YxyLessonsTopicLessonRepository;

/**
 * 专题课程--列表页--按一级、二级、三级、是否付费 进行课程查询
 * 
 * @author LiuWeidong
 *
 */
@CrossOrigin
@RestController
public class KechengTopicListSrhController extends YxyApiBaseController {

    @Autowired
    private YxyLessonsTopicLessonRepository yxyLessonsTopicLessonRepository;

    @Autowired
    private YxyLessonsTopicCatalogRepository yxyLessonsTopicCatalogRepository;

    @Autowired
    JdbcTemplate JdbcTemplate;

    @RequestMapping(value = "/api_yw/home/kecheng/topic/list/srh", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Page<RespModel> find(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {

        String oneId = reqModel.oneId;
        String twoId = reqModel.twoId;
        String threeId = reqModel.threeId;
        Integer vip = reqModel.vip;
        String lastModifiedDate = reqModel.lastModifiedDate;
        String ep = reqModel.ep;
        String pv = reqModel.pv;
        String name = (StringUtils.isNotBlank(reqModel.name)) ? "%" + reqModel.name + "%" : "";

        Pageable pageable = reqModel.getPageable(Direction.DESC, "createdDate");

        StringBuffer sb = new StringBuffer();
        sb.append(" SELECT                          ");
        sb.append("   id                            ");
        sb.append(" FROM                            ");
        sb.append("   yxy_lessons.yxy_lessons_topic_lesson    ");
        sb.append(" WHERE 1 = 1                     ");
        sb.append(" and status = 2 ");
        sb.append(" and is_visible = 1 ");

        List<Object> paramList = new ArrayList<>();

        if (StringUtils.isNotBlank(oneId)) {
            sb.append("   AND catalog_id1 = ?                            ");
            paramList.add(oneId);
        }
        if (StringUtils.isNotBlank(twoId)) {
            sb.append("   AND catalog_id2 = ?                            ");
            paramList.add(twoId);
        }
        if (StringUtils.isNotBlank(threeId)) {
            sb.append("   AND catalog_id3 = ?                            ");
            paramList.add(threeId);
        }
        if (null != vip) {
            sb.append("   AND vip = ?                            ");
            paramList.add(vip);
        }
        if (StringUtils.isNotBlank(reqModel.name)) {
            sb.append("   AND name like ?                                ");
            paramList.add(name);
        }

        List<String> countList = JdbcTemplate.queryForList(sb.toString(), paramList.toArray(), String.class);

        // 最近更新、评分最高、学习最多 排序
        if (StringUtils.isNotBlank(lastModifiedDate)) {
            sb.append(" ORDER BY up_date DESC  ");
        }
        if (StringUtils.isNotBlank(ep) && StringUtils.isBlank(lastModifiedDate)) {
            sb.append(" ORDER BY ep DESC  ");
        }
        if (StringUtils.isNotBlank(pv) && StringUtils.isBlank(ep) && StringUtils.isBlank(lastModifiedDate)) {
            sb.append(" ORDER BY vv DESC ");
        }

        // sb.append(" limit " + 1 + "," + 10);
        sb.append(" limit " + pageable.getOffset() + "," + pageable.getPageSize());
        List<String> idList = JdbcTemplate.queryForList(sb.toString(), paramList.toArray(), String.class);

        List<RespModel> returnLst = new ArrayList<>();
        for (String string : idList) {
            RespModel model = new RespModel();
            YxyLessonsTopicLesson yxyLessonsTopicLessons = yxyLessonsTopicLessonRepository.findById(string);

            YxyLessonsTopicCatalog yxyLessonsTopicCatalogs = yxyLessonsTopicCatalogRepository.findByid(yxyLessonsTopicLessons.getCatalogId3());

            if (null != yxyLessonsTopicCatalogs) {
                model.threeName = yxyLessonsTopicCatalogs.getName();
                model.name = yxyLessonsTopicLessons.getName();
                model.vip = yxyLessonsTopicLessons.getVip();
                model.cover = yxyLessonsTopicLessons.getCover();
                model.count = yxyLessonsTopicLessons.getCount();
                model.duration = yxyLessonsTopicLessons.getDuration();
                model.ep = yxyLessonsTopicLessons.getEp() == null ? 0.0f : yxyLessonsTopicLessons.getEp();
                model.vv = yxyLessonsTopicLessons.getVv();
                model.lessonId = yxyLessonsTopicLessons.getId();
                returnLst.add(model);
            }

        }

        Page<RespModel> page = new PageImpl<>(returnLst, pageable, countList.size());
        return page;
    }

    /* 请求参数 */
    protected static class ReqModel extends YxyApiPageReqModel {

        // 一级ID
        public String oneId;

        // 二级ID
        public String twoId;

        // 三级ID
        public String threeId;

        // 是否vip
        public Integer vip;

        public Integer pageNum;

        // 课程名称
        public String name;

        // 按最近更新排序(如果点击前台传“1”)
        public String lastModifiedDate;

        // 按评分最高排序(如果点击前台传“1”)
        public String ep;

        // 按学习最多排序(如果点击前台传“1”)
        public String pv;
    }

    /* 响应参数 */
    protected static class RespModel extends YxyApiRespModel {

        // 三级分类名称
        public String threeName;

        // 是否vip 0--非vip 1--vip课程
        public Integer vip;
        // 封面
        public String cover;
        // 总课时
        public Integer count;
        // 总时长
        public Integer duration;
        // 课程名称
        public String name;
        // 课程评分（所有评分之和处于评分人数即综合评分）
        public Float ep;
        // 学习人数
        public Integer vv;
        // 课程Id
        public String lessonId;
    }
}
